//
//  SSEmoticonCell.swift
//  表情键盘
//
//  Created by 叶竞泽 on 2017/2/9.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

/// 表情Cell的代理协议
protocol SSEmoticonCellDelegate: NSObjectProtocol {
    
    /// 表情Cell选中表情模型
    ///
    /// - Parameter emoticon: 表情模型 / nil表示删除
    func emoticonCellDidSelectedEmoticon(cell: SSEmoticonCell, emoticon: SSEmoticon?)
}

/// 表情页面的cell,每一个cell显示20个表情 - 每一个cell中用九宫格算法,自行添加20个表情,第21个位置放置删除按钮
class SSEmoticonCell: UICollectionViewCell {
 
    weak var delegate: SSEmoticonCellDelegate?
    
    /// 当前页面的表情模型数组,最多20个
    var emoticons: [SSEmoticon]? {
        
        didSet {
            
            // 1.隐藏所有的按钮
            for button in contentView.subviews {

                button.isHidden = true
                
                if button == contentView.subviews.last as? UIButton {
                    if emoticons?.count ?? 0 > 0 {
                        button.isHidden = false
                    }
                }
            }
            
            // 2.遍历表情模型数组,设置按钮图片
            for (i,emoticon) in (emoticons ?? []).enumerated() {
                
                // 1.取出按钮
                if let button = contentView.subviews[i] as? UIButton {
                    
                    // 2.设置按钮
                    button.setImage(emoticon.image, for: .normal)
                    button.isHidden = false
                    button.setTitle(emoticon.emoji, for: .normal)
                }
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听方法
    
    /// 监听表情和删除按钮的点击
    ///
    /// - Parameter sender: 按钮
    @objc fileprivate func emoticonClick(sender: UIButton) {
        
        var emoticon: SSEmoticon?
        
        // 按钮的tag如果是20是删除按钮
        
        if sender.tag < emoticons?.count ?? 20 {

            // 点击了表情
            emoticon = emoticons?[sender.tag]
            
            
        } else {
            
            // 点击了删除
        }
        
        delegate?.emoticonCellDidSelectedEmoticon(cell: self, emoticon: emoticon)
        
    }
}

// MARK: - 设置界面
extension SSEmoticonCell {
    
    func setupUI() {
        
        let rowCount = 3
        let colCount = 7
        // 左右间距
        let leftMargin: CGFloat = 8
        // 底部间距
        let bottomMargin: CGFloat = 16
        
        let w = (bounds.width - 2 * leftMargin) / CGFloat(colCount)
        let h = (bounds.height - bottomMargin) / CGFloat(rowCount)
        
        
        
        // 1.连续创建21个按钮
        for i in 0 ..< 21 {
            
            let row = i / colCount
            let col = i % colCount
            
            // 创建按钮
            let button = UIButton()
            // 设置按钮的frame
            let x = leftMargin + CGFloat(col) * w
            let y = CGFloat(row) * h
            button.frame = CGRect(x: x, y: y, width: w, height: h)
            // 设置按钮的属性
            button.backgroundColor = button.superview?.backgroundColor
            button.titleLabel?.font = UIFont.systemFont(ofSize: 32)
            button.isUserInteractionEnabled = true
            // 设置按钮的tag
            button.tag = i
            // 设置按钮的监听方法
            button.addTarget(self, action: #selector(emoticonClick(sender:)), for: .touchUpInside)
            contentView.addSubview(button)
        }
        
        // 2.设置删除按钮
        let removeButton = contentView.subviews.last as? UIButton
        // 删除按钮图像
        let deleteNormalImage = UIImage(named: "compose_emotion_delete", in: SSEmoticonManager.shared.bundle, compatibleWith: nil)
        let deleteHighlightedImage = UIImage(named: "compose_emotion_delete_highlighted", in: SSEmoticonManager.shared.bundle, compatibleWith: nil)
        removeButton?.setImage(deleteNormalImage, for: .normal)
        removeButton?.setImage(deleteHighlightedImage, for: .highlighted)
        removeButton?.setImage(deleteHighlightedImage, for: .selected)
    }
    
    
}
